//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "IntervalPrice.h"
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::CIntervalPrice::CIntervalPrice (Double open, Double close, Double high, Double low) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phIntervalPrice = NULL;
#endif
        QuantLib::Real _open = (QuantLib::Real)ValueHelper::Convert (open);
        QuantLib::Real _close = (QuantLib::Real)ValueHelper::Convert (close);
        QuantLib::Real _high = (QuantLib::Real)ValueHelper::Convert (high);
        QuantLib::Real _low = (QuantLib::Real)ValueHelper::Convert (low);
        _ppIntervalPrice = new boost::shared_ptr<QuantLib::IntervalPrice> (new QuantLib::IntervalPrice ( _open,  _close,  _high,  _low ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CIntervalPrice::CIntervalPrice () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phIntervalPrice = NULL;
#endif
        _ppIntervalPrice = new boost::shared_ptr<QuantLib::IntervalPrice> (new QuantLib::IntervalPrice ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CIntervalPrice::CIntervalPrice (boost::shared_ptr<QuantLib::IntervalPrice>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntervalPrice = NULL;
#endif
	_ppIntervalPrice = &childNative;
    
}
Cephei::QL::CIntervalPrice::CIntervalPrice (QuantLib::IntervalPrice& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phIntervalPrice = NULL;
#endif
	_ppIntervalPrice = new boost::shared_ptr<QuantLib::IntervalPrice> (&childNative);
    
    _IntervalPriceOwner = owner;
    
}

Cephei::QL::CIntervalPrice::CIntervalPrice (CIntervalPrice^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntervalPrice = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIntervalPrice = new boost::shared_ptr<QuantLib::IntervalPrice> (copy->GetShared());
        
    }
}
Cephei::QL::CIntervalPrice::CIntervalPrice (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntervalPrice = NULL;
#endif
	if (!t->IsSubclassOf(CIntervalPrice::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CIntervalPrice::CIntervalPrice (QuantLib::Handle<QuantLib::IntervalPrice>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIntervalPrice = &childNative;
	_ppIntervalPrice = &static_cast<boost::shared_ptr<QuantLib::IntervalPrice>>(childNative.currentLink());
    
    _IntervalPriceOwner = owner;
}
Cephei::QL::CIntervalPrice::CIntervalPrice (QuantLib::Handle<QuantLib::IntervalPrice> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phIntervalPrice = &childNative;
	_ppIntervalPrice = &static_cast<boost::shared_ptr<QuantLib::IntervalPrice>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CIntervalPrice::CIntervalPrice (QuantLib::IntervalPrice childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phIntervalPrice = NULL;
#endif
	_ppIntervalPrice = new boost::shared_ptr<QuantLib::IntervalPrice> (new QuantLib::IntervalPrice (childNative));
    
}
#endif

Cephei::QL::CIntervalPrice::~CIntervalPrice ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppIntervalPrice != NULL)
    {
	    delete _ppIntervalPrice;
        _ppIntervalPrice = NULL;
    }
}
Cephei::QL::CIntervalPrice::!CIntervalPrice ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppIntervalPrice != NULL)
    {
	    delete _ppIntervalPrice;
    }
}
QuantLib::IntervalPrice& Cephei::QL::CIntervalPrice::GetReference ()
{
    if (_ppIntervalPrice == NULL) throw gcnew NativeNullException ();
	return **_ppIntervalPrice;
}
boost::shared_ptr<QuantLib::IntervalPrice>& Cephei::QL::CIntervalPrice::GetShared ()
{
    if (_ppIntervalPrice == NULL) throw gcnew NativeNullException ();
	return *_ppIntervalPrice;
}
QuantLib::IntervalPrice* Cephei::QL::CIntervalPrice::GetPointer ()
{
    if (_ppIntervalPrice == NULL) throw gcnew NativeNullException ();
	return &**_ppIntervalPrice;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::IntervalPrice>& Cephei::QL::CIntervalPrice::GetHandle ()
{
	if (_phIntervalPrice == NULL)
	{
		_phIntervalPrice = new Handle<QuantLib::IntervalPrice> (*_ppIntervalPrice);
	}
	return *_phIntervalPrice;
}
#endif
bool Cephei::QL::CIntervalPrice::HasNative () 
{
	return (_ppIntervalPrice != NULL);
}

Double Cephei::QL::CIntervalPrice::Close::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIntervalPrice)->close ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CIntervalPrice::High::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIntervalPrice)->high ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CIntervalPrice::Low::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIntervalPrice)->low ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CIntervalPrice::Open::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIntervalPrice)->open ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IIntervalPrice^ Cephei::QL::CIntervalPrice::SetValue (Double value, QL::IntervalPrice::TypeEnum t)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _value = (QuantLib::Real)ValueHelper::Convert (value);
        QuantLib::IntervalPrice::Type _t = (QuantLib::IntervalPrice::Type)t ;
    	(*_ppIntervalPrice)->setValue ( _value,  _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IIntervalPrice^ Cephei::QL::CIntervalPrice::SetValues (Double open, Double close, Double high, Double low)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _open = (QuantLib::Real)ValueHelper::Convert (open);
        QuantLib::Real _close = (QuantLib::Real)ValueHelper::Convert (close);
        QuantLib::Real _high = (QuantLib::Real)ValueHelper::Convert (high);
        QuantLib::Real _low = (QuantLib::Real)ValueHelper::Convert (low);
    	(*_ppIntervalPrice)->setValues ( _open,  _close,  _high,  _low );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CIntervalPrice::Value (QL::IntervalPrice::TypeEnum t)
{
    try
    {
        QuantLib::IntervalPrice::Type _t = (QuantLib::IntervalPrice::Type)t ;
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppIntervalPrice)->value ( _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IIntervalPrice^ Cephei::QL::CIntervalPrice_Factory::Create (Double open, Double close, Double high, Double low)
{
    return gcnew CIntervalPrice ( open,  close,  high,  low);
}
Cephei::QL::IIntervalPrice^ Cephei::QL::CIntervalPrice_Factory::Create ()
{
    return gcnew CIntervalPrice ();
}
